﻿using System.Collections.Generic;

//using System.Web.Mvc;
//using ContactManager.Models.Validation;
using ContactManager.Models.Entities;
using System;
//using System.Linq;
using System.Text;
//using System.Text.RegularExpressions;
//using StructureMap;
using ContactManager.Models.Repositories;
//using NHibernate.Criterion;


namespace ContactManager.Models
{
    public class ContactManagerService : IContactManagerService
    {
        //private IModelStateWrapper _validationDictionary;
        private IContactManagerRepositoryAr _repository;// = new IContactManagerRepositoryAr();
        private IGroupManagerRepository _groupRepository;

 //       [StructureMap.DefaultConstructor]
        //public ContactManagerService(IModelStateWrapper validationDictionary)
        //    : this(validationDictionary, new ContactManagerRepository())
        //{ }
        //EMPTY CONSTRUCTOR NOT NEEDED as it is managed by StructureMap
        //public ContactManagerService()
        //            {  _repository = new ContactManagerRepository();}


        //public ContactManagerService(IModelStateWrapper validationDictionary, IContactManagerRepository repository)
        //public ContactManagerService(IModelStateWrapper validationDictionary, IContactManagerRepositoryAr repository, IGroupManagerRepository groupRepository)
        public ContactManagerService(IContactManagerRepositoryAr repository, IGroupManagerRepository groupRepository)

        {
            //_validationDictionary = validationDictionary;
            _repository = repository;
            _groupRepository = groupRepository;
        }
        
    #region IContactManagerService Members
        #region Contact Methods
        //GLOBAL METHODS
        public Contact GetContact(Guid id)
        {
            Contact contact = _repository.LoadById<Contact>(id);
            return contact;
            // return _repository ContactManagerRepositoryAr.GetContact(id);
             
        }        
        public IList<Contact> ListContacts()
        {
            IList<Contact> contacts = _repository.ListAll<Contact>();
            return contacts;
            //return ContactManagerRepositoryAr.ListContacts().ToList<Contact>();
        }
        public IList<Contact> FindAllContacts()
        {
            IList<Contact> contacts = _repository.ListAll<Contact>();
            return contacts;
        }
        public IList<Contact> FindTodayContacts()
        {
            ///!!!!!!!!!!!!!!!!!!!!!!!!!!
            IList<Contact> contacts = _repository.ListAll<Contact>();
            return contacts;
            //return ContactManagerRepositoryAr.FindTodayContacts();
        }
        //INTANCE METHODS
        public bool Add(Contact contactToCreate)
        {
            // Validation logic MOVED AWAY
            //if (!ValidateContact(contactToCreate)) return false;

            // Database logic
            Guid newID = Guid.NewGuid();
            contactToCreate.Id = newID;
            contactToCreate.DateEntered = DateTime.Today;
            try
            {
                _repository.Create<Contact>(contactToCreate);
               
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool UpdateInstance <T,Y> (Guid instanceID,T instanceToUpdate) where T: class where Y : class
        {
            //Load Group Instance
            if(typeof(Y) == typeof(ContactGroup) ) 
            {
                Y contactGroup = _groupRepository.LoadById<Y>(instanceID); 
                
            }
            
            
            return true;

        }

        public IList<Contact> FindContactsBy(char initial, string field)
        {           
            IList<Contact> contacts =  _repository.FindContactsBy(initial,field);
            return contacts;                    
        }


        public IList<Contact> FindContactsBetweenDates(string field, DateTime date1, DateTime date2)
        {
            IList<Contact> contacts = _repository.FindContactsBetweenDates(field,date1,date2);
            return contacts;
        }
        

        public bool EditContact(Contact contactToEdit)
        {
          
            try
            {
                _repository.Edit<Contact>(contactToEdit);
            }
            catch(Exception e)
            {
                Exception _e = e;
                return false;
            }
            return true;

        }
        public bool DeleteContact(Contact contactToDelete)
        {
            try
            {

                _repository.Delete<Contact>(contactToDelete);
                
            }
            catch
            {
                return false;
            }
            return true;
        }
        #endregion

        #region ContactGroup Methods

        // ContactGroup methods
        //GLOBAL METHODS

        
        public Entities.ContactGroup GetGroup(Guid id)
        {
            if (id.ToString().Length > 0){return _groupRepository.LoadById<ContactGroup>(id);}
            else{return null;}
        }
        public ContactGroup GetFirstGroup() 
        {            
            return _repository.GetFirstGroup();
        }
        public IList<Entities.ContactGroup> FindAllGroups()
        {
            IList<ContactGroup> contactGroups = _groupRepository.ListAll<ContactGroup>();
            return contactGroups;
        }
        public IList<Entities.ContactGroup> ListGroups()
        {
            IList<ContactGroup> contactGroups = _groupRepository.ListAll<ContactGroup>();
            return contactGroups;
        }
        
        //INSTANCE METHODS
        public bool EditGroup(Entities.ContactGroup groupToEdit)
        {
            // Validation logic
            if (!ValidateGroup(groupToEdit))
                return false;

            // Database logic
            try
            {
                _groupRepository.Edit<ContactGroup, ContactGroup>(groupToEdit, groupToEdit);
                
            }
            catch
            {
                return false;
            }
            return true;

        }
        public bool CreateGroup(Entities.ContactGroup groupToCreate)
        {
            //// Validation logic
            //if (!ValidateGroup(groupToCreate))
            //    return false;

            // Database logic
            Guid newID = Guid.NewGuid();
            groupToCreate.Id = newID;
            try
            {
                _groupRepository.Create<ContactGroup>(groupToCreate);
                //ContactManagerRepositoryAr.CreateGroup(groupToCreate);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool DeleteGroup(Entities.ContactGroup groupToDelete)
        {
            try
            {
                _groupRepository.Delete<ContactGroup>(groupToDelete);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool ValidateGroup(Entities.ContactGroup groupToValidate)
        {
            if (groupToValidate.Name.Trim().Length == 0) return false;
                //_validationDictionary.AddError("Name", "Name is required.");
            //return _validationDictionary.IsValid;
            return true;
        }
        #endregion
    #endregion
    }
   

}


